<?php 

include_once ('dao_conv.php');
include_once ('dao_common.php');
include_once ('dao_xml.php');

//意图，封装接口包的结构细节
/*
基本格式
type  : string
param : 通过field-->value映射的参数列表，比如 <retcode>0</retcode>
data : 表结构对象，数组，包含多个data节点 ,每个data包含field->value的映射。
这样设计的意图是因为工单系统的业务接口基本上可以直接映射到二维表的增改查。
*/
class DAO_Package
{
	static $header_tag = array(  'encode'=>1,
	'charset'=>1,
	'retcode'=>1,
	'reason'=>1,
	'datacount'=>1,
	'cache'=>1,
	'dataload'=>1,
	'style'=>1,
	'data_encode'=>1
	);
	static public  $TAG_DATA='data';
	static public  $TAG_HEADER='header';
	static public  $TAG_FIELD='field';
	public		$type='';
	public      $format='';      // 'XML'/'SOAP'/'TEXT'
	public		$header=array();/*encode,retcode,reason,datacount....etc*/
	public		$field=array();
	public		$data=array();

	public function __construct()
	{
		$this->init();
	}

	public function __destruct()
	{
	}

	public function init()
	{
		$this->type='daopackage';
		unset($this->header);
		$this->header=array();
		unset($this->field);
		$this->field=array();
		unset($this->data);
		$this->data=array();
		$this->header['charset']='UTF-8';//默认编码UTF8,如使用其他编码需要修改
	}

	public function is_data_empty()
	{
		if(sizeof($this->data)==0)
		{
			return true;
		}
		if(sizeof($this->data[0])==0)
		{
			return true;
		}
		return false;
	}

	public function get_header($key)
	{
		if(array_key_exists($key,$this->header))
		{
			return $this->header[$key];
		}
		else
		{
			return'';
		}
	}

	public function set_header($key,$value)
	{
		$this->header[$key]=$value;
	}


	public function set_data($index,$name,$value,$skip_null=false)
	{
		if((!isset($value)||$value==NULL)&&$skip_null==true)
		{
			return;
		}
		$this->data[$index][$name] = $value;
	}

	public function get_resp_type($req_type='')
	{
		if(strlen($req_type)==0)
		{
			$req_type=$this->type;
		}
		$pos = strpos($req_type,'_req');
		if($pos===false)
		{
			return $req_type;
		}
		return substr($req_type,0,$pos).'_resp';
	}

	public function get_field_name($field)
	{
		if(is_string($field))
		{
			return $field;
		}
		return $field->name;
	}

	public function to_string($format=NULL)
	{
		if($format==NULL)
		{
			$format = $this->format;
		}
		if(strlen($format)==0)
		{
			$format='XML';
		}

		$charset='gb2312';

		if(strcmp($this->header['charset'],'GBK')==0)
		{
			$charset = 'gb2312';
		}
		else
		{
			$charset = 'UTF-8';
			$this->header['charset']=$charset;
			array_iconv($this->header,$conv_rule);
		}
		DAO_Log::debug(__FUNCTION__.":format = $format,package_charset = ".$this->header['charset']." response encode=$charset");
		if($format=='SOAP')
		{
			$msg = "<?xml version=\'1.0\' encoding=\'$charset\' ?>\r\n";
			$msg.=to_soap($this,$this->get_header('style'));
			return $msg;
		}
		if($format=='XML')
		{
			$msg = "<?xml version=\'1.0\' encoding=\'$charset\' ?>\r\n";
			$msg .=to_xml($this,$this->get_header('style'));
			return $msg;
		}
		if($format=='TEXT')
		{
			return $this->data;
		}
	}

	public function count_data_load()
	{
		$datalen = 0;
		foreach ($this->header as $k=>$v)
		{
			$datalen +=strlen($v);
		}

		if(isset($this->field)&&sizeof($this->field)>0)
		{
			foreach ($this->field as $v)
			{
				$datalen +=strlen($v);
			}
			$msg.=$crlf;
		}
		foreach($this->data as $k=>$v)
		{
			if(isset($v))
			{
				foreach ($v as $f=>$value)
				{
					$datalen +=strlen($value);
				}
			}
		}
		$xmllen = strlen(to_xml($this,$this->get_header('style')));
		$load['datalen']=$datalen;
		$load['xmllen']=$xmllen;
		$load['load']=$datalen/$xmllen;
		return $load;
	}

	/*将对象dump到标准输出和字符串
	用于调试，可以直接返回提高效率
	*/
	public function dump($crlf="\r\n",$echo=true)
	{
		$msg='';
		$msg.="$crlf--Type=".$this->type.$crlf;
		$msg.= '--Headers:'.$crlf;
		foreach ($this->header as $k=>$v)
		{
			$msg.= "\t".$k.'='.$v.$crlf;
		}
		$msg=$msg. '--Fields:'.$crlf;
		if(isset($this->field)&&sizeof($this->field)>0)
		{
			foreach ($this->field as $v)
			{
				$msg.="\t".$v.';';
			}
			$msg.=$crlf;
		}
		$msg=$msg. '--Data:'.$crlf;
		foreach($this->data as $k=>$v)
		{
			$msg.="\t".$k.']';
			if(isset($v))
			{
				foreach ($v as $f=>$value)
				{
					$msg.=$f.'=';
					$msg.=$value;
					$msg.=';';
				}
			}
			$msg.=$crlf;
		}
		if($echo)echo $msg;
		return $msg;
	}

	function encode()
	{
		//encode和charset同义，内部采用charset,这一段是兼容代码
		if(strlen($this->header['charset'])>0)
		{
			$this->header['encode'] = $this->header['charset'];
		}
		//-------------------------------------
		array_encode($this->header,$this->header['data_encode'],$this->header['charset']);
		matrix_encode($this->data,$this->header['data_encode'],$this->header['charset']);
	}
	
	function decode()
	{
		//encode和charset同义，内部采用charset,这一段是兼容代码
		if(strlen($this->header['encode'])>0)
		{
			$this->header['charset'] = $this->header['encode'];
		}
		//-----------------

		include_once('dao_conv.php');
		array_decode($this->header,$this->header['data_encode'],$this->header['charset']);
		matrix_decode($this->data,$this->header['data_encode'],$this->header['charset']);

	}
}
?>
